package com.john.study.es.config.matcher;

import com.john.common.util.CollectionUtil;
import com.john.common.util.PatternUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by jiangguangtao on 2016/6/18.
 */
public class RegexPatternMatcher implements PatternMatcher {
    private int matchFlags = Pattern.CASE_INSENSITIVE;

    private Pattern[] compiledClassNamePatterns = new Pattern[0];
    private Pattern[] compiledExcludeClassNamePatterns = new Pattern[0];

    private String[] includeFilters; //要包含的类名称规则列表
    private String[] excludeFilters; //要过滤的类名称规则

    public RegexPatternMatcher() {
    }

    public RegexPatternMatcher(String[] includeFilters) {
        setIncludeFilters(includeFilters);
    }

    public RegexPatternMatcher(String[] includeFilters, String[] excludeFilters) {
        setIncludeFilters(includeFilters);
        setExcludeFilters(excludeFilters);
    }

    public RegexPatternMatcher(List<String> includeFilterList, List<String> excludeFilterList) {
        if(CollectionUtils.isNotEmpty(includeFilterList)) {
            includeFilterList = CollectionUtil.uniqueList(includeFilterList);
            String[] includeFilters = includeFilterList.toArray(new String[includeFilterList.size()]);
            setIncludeFilters(includeFilters);
        }
        if(CollectionUtils.isNotEmpty(excludeFilterList)) {
            excludeFilterList = CollectionUtil.uniqueList(excludeFilterList);
            String[] excludeFilters = excludeFilterList.toArray(new String[excludeFilterList.size()]);
            setExcludeFilters(excludeFilters);
        }
    }

    public int getMatchFlags() {
        return matchFlags;
    }

    /**
     * 设置正则表达式匹配的规则标志 默认为 Pattern.CASE_INSENSITIVE
     *
     * @param matchFlags
     */
    public void setMatchFlags(int matchFlags) {
        this.matchFlags = matchFlags;
    }

    public String[] getIncludeFilters() {
        return includeFilters;
    }

    public void setIncludeFilters(String... includeFilters) {
        if(null == includeFilters || includeFilters.length==0) {
            return;
        }
        this.includeFilters = includeFilters;
        this.compiledClassNamePatterns = PatternUtil.compilePatterns(includeFilters, matchFlags);
    }

    public String[] getExcludeFilters() {
        return excludeFilters;
    }

    /**
     * 设置匹配的排除规则，可以不设置的
     *
     * @param excludeFilters
     */
    public void setExcludeFilters(String... excludeFilters) {
        if(null == excludeFilters || excludeFilters.length==0) {
            return;
        }
        this.excludeFilters = excludeFilters;
        this.compiledExcludeClassNamePatterns = PatternUtil.compilePatterns(excludeFilters, matchFlags);
    }

    @Override
    public boolean matches(String source) {
        if (StringUtils.isBlank(source)) {
            return false;
        }
        if (null != compiledClassNamePatterns && compiledClassNamePatterns.length > 0) {
            for (Pattern pattern : compiledClassNamePatterns) {
                if (pattern.matcher(source).matches()) {
//                    return true;
                    //判断排除规则
                    if (null != compiledExcludeClassNamePatterns && compiledExcludeClassNamePatterns.length > 0) {
                        for (Pattern expa : compiledExcludeClassNamePatterns) {
                            if (expa.matcher(source).matches()) {
                                return false;//虽然包含匹配成功，但是包含在排除规则内了，一样不匹配
                            }
                        }
                        return true;
                    } else {
                        //如果无排除规则，直接返回匹配的TRUE
                        return true;
                    }
                }
            }

        }

        return false;
    }
}
